package cn.xny.basic.controller;


import cn.xny.basic.util.AjaxResult;
import cn.xny.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {



    //文件上传
    @PostMapping
    public AjaxResult upload(@RequestPart MultipartFile file){

        try {

            String oName = file.getOriginalFilename();//xxx.png
            String exeName= oName.substring(oName.lastIndexOf(".") + 1);//png

            //调用工具类
            String fileName = FastDfsUtils.upload(file.getBytes(), exeName);
              return AjaxResult.me().setObj(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }

    }
    //文件删除
     @DeleteMapping
    public AjaxResult deleteFile(@RequestParam("path") String path){

         try {
                                     //path=/group1/M00/03/8C/CgAIC2PeOEiATbxZAAFFOEExh44883.jpg
             path = path.substring(1);// group1/M00/03/8C/CgAIC2PeOEiATbxZAAFFOEExh44883.jpg
             String groupName = path.substring(0, path.indexOf("/"));//group1
             String  fileName = path.substring(path.indexOf("/")+1);// M00/03/8C/CgAIC2PeOEiATbxZAAFFOEExh44883.jpg
             FastDfsUtils.delete(groupName,fileName);
              return  new AjaxResult();
         } catch (Exception e) {
             e.printStackTrace();
             return new AjaxResult(false,"删除失败");
         }


     }

}
